package com.vip.admin.modules.system.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vip.admin.common.annotation.OperationLog;
import com.vip.admin.common.constant.CommonConstant;
import com.vip.admin.common.entity.Result;
import com.vip.admin.modules.system.entity.SysRole;
import com.vip.admin.modules.system.service.ISysRoleService;
import com.vip.admin.modules.system.service.ISysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 袜子离不开秋裤
 * @since 2020-01-08
 */
@RestController
@RequestMapping("/system")
@Api(tags = "系统用户管理")
public class SysUserController {

	@Autowired
	ISysUserService sysUserService;
	@Autowired
	ISysRoleService sysRoleService;

	@GetMapping("/delUser")
	@ApiOperation("删除用户")
	public Result<?> deleteUser(@RequestParam(name = "id", required = true) @ApiParam("标识id") Integer id) {
		return sysUserService.removeById(id) ? Result.ok() : Result.error("删除失败！");
	}

	@GetMapping(value = "/getUser", produces = "application/json;charset=UTF-8")
	@ApiOperation("获取用户")
	@OperationLog(logContent = "获取用户信息", operateType = CommonConstant.OPERATE_TYPE_SELECT)
	public Result<?> getUser(@RequestParam(name = "id", required = true) @ApiParam("标识id") Integer id) {
		Subject currentUser = SecurityUtils.getSubject();
		return Result.ok(sysUserService.getById(id));
	}

	@RequiresPermissions(value = { "sys:user:list" })
	@GetMapping("list")
	@ApiOperation("获取用户列表")
	public Result<IPage<SysRole>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		Result<IPage<SysRole>> result = new Result<IPage<SysRole>>();
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
		Page<SysRole> page = new Page<SysRole>(pageNo, pageSize);
		IPage<SysRole> pageList = sysRoleService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setData(pageList);
		return result;
	}

}
